package util;

import javax.swing.JFrame;

import model.Massage;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.awt.*;
import java.util.List;

public class DrawUtil {

    public static void plot(List<Massage> list, double samplingTime) {
        XYSeries theta_series = new XYSeries("theta:摆角     ");
        XYSeries x_series = new XYSeries("x: 小车位置     ");
        XYSeries x_tar_series = new XYSeries("x_target: 目标位置    ");
        for (int i = 0; i < list.size(); i++) {
            double x = i * samplingTime;
            theta_series.add(x,list.get(i).getTheta());
            x_series.add(x,list.get(i).getX_car());
            x_tar_series.add(x,list.get(i).getX_tar());
        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(theta_series);
        dataset.addSeries(x_series);
        dataset.addSeries(x_tar_series);
        JFreeChart chart = ChartFactory.createXYLineChart(
                "The pendulum function graph", // chart title
                "x", // x axis label
                "y", // y axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // include legend 是否显示图例
                false, // tooltips
                false // urls
        );
        //设置标题字体
        chart.setTitle(new TextTitle("The pendulum function graph", new Font("幼圆",Font.BOLD,16)));
        //防止乱码
        chart.getLegend().setItemFont(new Font("幼圆",Font.PLAIN,14));
        //设置画布颜色
        chart.setBackgroundPaint(Color.white);

        XYPlot plot = (XYPlot)chart.getPlot();

        //设置x轴字体
        ValueAxis valueAxis=plot.getDomainAxis();
        valueAxis.setLabelFont(new Font("幼圆",Font.BOLD,14));

        // 设置y轴上的标题字体
        NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
        numberAxis.setLabelFont(new Font("幼圆",Font.BOLD,14));

        StandardXYItemRenderer renderer = new StandardXYItemRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        renderer.setSeriesPaint(1, new Color(255,215,0));
        renderer.setSeriesPaint(2, new Color(0,139,69));
        plot.setRenderer(renderer);
        ChartFrame frame = new ChartFrame("倒立摆状态函数图像", chart);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}